/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __t3_browsemsg_resp.c                               */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/30 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <t3/t3_core.h>
#include <t3/t3_analysis.h>
#include <t3/t3_ejb.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/t3/config.h"
#include "../../../src/lib/report/frreport_dbg.h"

extern int __t3_browsemsg_resp(L7session_t3_private *p_st_private
                                                          , Frbyte_Packet *p_st_data
                                                          , Frreport_body_data *p_st_body_data
                                                          , int *i_need_report);
extern L7session_t3_private* __t3_private_create(void);


static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    L7session_t3_private *p_st_private = NULL;
    Conntrack *p_st_conn = NULL;
    Frreport_data *p_st_report_data = NULL;
    char *p_show_data = NULL;
    unsigned int ui_show_data_len = 0;
    int i_need_report = 0;
    int i_ret = 0;

    p_st_private = __t3_private_create();

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    //测试修改部分
    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x00\x00\x06\x01\x01\x28\x04\x02\x07\x00\x00\x00\x00\x0c\x00\x00\x01\x51\x60\x51\xb3\xdb\x00\x00\x00\x00\x00\x07\xf1\xb1\x03\x01\x82\x00\x00\x00\x04\x74\x65\x78\x74\x01\xfe\x00\xff", 55);

    i_ret = __t3_browsemsg_resp(p_st_private, p_st_data, p_st_body_data, &i_need_report);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_need_report, 1);

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);
    frreport_data_add_body(p_st_report_data, p_st_body_data);
    frdbg_report_data_format(p_st_report_data, FR_YES, &p_show_data, &ui_show_data_len);
    printf("p_show_data:[%s]\n", p_show_data);

    bytebuff_free(&p_st_data);
    frfree(p_st_conn);
    t3_session_destroy(p_st_private);
    frreport_data_free(&p_st_report_data);
    frfree(p_show_data);

    return;
}

static void case2(void)
{
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    L7session_t3_private *p_st_private = NULL;
    Conntrack *p_st_conn = NULL;
    int i_need_report = 0;
    int i_ret = 0;

    p_st_private = __t3_private_create();

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;
    p_st_conn->st_l7_analyzer.p_st_l7_module = (L7_module *)"1";

    p_st_body_data = frreport_body_new_l7(p_st_conn);

    //测试修改部分
    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x10\x0c\x01\xfe\x00\xff", 16);

    i_ret = __t3_browsemsg_resp(p_st_private, p_st_data, p_st_body_data, &i_need_report);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_need_report, 0);

    bytebuff_free(&p_st_data);
    frfree(p_st_conn);
    frreport_body_free(&p_st_body_data);
    t3_session_destroy(p_st_private);

    return;
}

int add_suite__t3_browsemsg_resp(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __t3_browsemsg_resp()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
